//
//  SGStory.m
//  iPing
//
//  Created by Thomas on 8/12/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "TCSGStory.h"


@implementation TCSGStory
@dynamic title;
@dynamic name;
@dynamic author;
@dynamic host;
@dynamic idNumber;
@dynamic HTMLCode;
@dynamic lastReadChapter;
@dynamic lastDownloadedChapter;
+(TCSGStory *)storyNoInsertFromDictionary:(NSDictionary *)infoDict 
						 withName:(NSString *)name 
		   inManagedObjectContext:(NSManagedObjectContext *)context
{
    // Create new molecule
	TCSGStory *story;/* = [NSEntityDescription insertNewObjectForEntityForName:@"SGStory" 
													 inManagedObjectContext:context];*/
	/*= [NSEntityDescription insertNewObjectForEntityForName:@"SGStory" 
	 inManagedObjectContext:context];*/
	NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
	NSEntityDescription *entity =[NSEntityDescription entityForName:@"TCSGStory"
											 inManagedObjectContext:context];
	
	[request setEntity:entity];
	
	NSPredicate *predicate =
	[NSPredicate predicateWithFormat:@"HTMLCode == %@", name];
	[request setPredicate:predicate];
	[request setFetchLimit:1];
	NSError *error = nil;
	NSArray *array = [context executeFetchRequest:request error:&error];
	if (array != nil) {
		int count = [array count]; // may be 0 if the object has been deleted
		int i;
		NSLog(@"not nil");
		NSLog(@"count: %i",count);
		if(count == 0)
		{
			NSLog(@"new item,%@",[infoDict objectForKey:TITLEKEY]);
			story = [NSEntityDescription insertNewObjectForEntityForName:@"TCSGStory" 
												  inManagedObjectContext:context];
		}
		else
		{
			story = [array objectAtIndex:0];
		}
		// …
	}
	else
	{
		story = [NSEntityDescription insertNewObjectForEntityForName:@"TCSGStory" 
											  inManagedObjectContext:context];
		story.host		= [infoDict objectForKey:HOSTKEY];
	}
	
	//NSLog(@"properties: %@", [story properties]);
	//story.name		= name;
	story.title		= [infoDict objectForKey:TITLEKEY];
	story.author	= [infoDict objectForKey:AUTHORKEY];
	story.host		= [infoDict objectForKey:HOSTKEY];
	story.idNumber	= [infoDict objectForKey:IDKEY];
	story.HTMLCode	= name;

	//NSLog(@"properties: %@", [story properties]);
	NSArray *keys	= [infoDict allKeys];
	if([keys containsObject:GENREKEY])
	{
		story.genre	= [infoDict objectForKey:GENREKEY];
	}
	if([keys containsObject:SUMMARYKEY])
	{
		story.summary	= [infoDict objectForKey:SUMMARYKEY];
	}
	if([keys containsObject:CODESKEY])
	{
		story.codes	= [infoDict objectForKey:CODESKEY];
	}
	
    return story;
}
+(void)removeObjectForHTMLCode:(NSString *)HTMLCode inContext:(NSManagedObjectContext *)context
{
	TCSGStory *story;/* = [NSEntityDescription insertNewObjectForEntityForName:@"SGStory" 
	 inManagedObjectContext:context];*/
	/*= [NSEntityDescription insertNewObjectForEntityForName:@"SGStory" 
	 inManagedObjectContext:context];*/
	NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
	NSEntityDescription *entity =[NSEntityDescription entityForName:@"TCSGStory"
											 inManagedObjectContext:context];
	[request setEntity:entity];
	
	NSPredicate *predicate =
	[NSPredicate predicateWithFormat:@"HTMLCode == %@", HTMLCode];
	[request setPredicate:predicate];
	[request setFetchLimit:1];
	NSError *error = nil;
	NSArray *array = [context executeFetchRequest:request error:&error];
	if (array != nil) {
		int count = [array count]; // may be 0 if the object has been deleted
		int i;
		NSLog(@"count: %i",count);
		if(count != 0)
		{
			NSLog(@"not nil");
			NSLog(@"found");
			story = [array objectAtIndex:0];
			[context deleteObject:story];
		}
		// …
	}
	
	
	
}
+(int)getLastReadChapterNumberForHTMLCode:(NSString *)HTMLCode inContext:(NSManagedObjectContext *)context;
{
	int i = 0;
	TCSGStory *story;
	NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
	NSEntityDescription *entity =[NSEntityDescription entityForName:@"TCSGStory"
											 inManagedObjectContext:context];
	[request setEntity:entity];
	
	NSPredicate *predicate =
	[NSPredicate predicateWithFormat:@"HTMLCode == %@", HTMLCode];
	[request setPredicate:predicate];
	[request setFetchLimit:1];
	NSError *error = nil;
	NSArray *array = [context executeFetchRequest:request error:&error];
	if (array != nil) {
		int count = [array count]; // may be 0 if the object has been deleted
		int i;
		NSLog(@"count: %i",count);
		if(count != 0)
		{
			NSLog(@"not nil");
			NSLog(@"found");
			story = [array objectAtIndex:0];
		}
		// …
	}
	return nil;//story.lastReadChapter;
	
	
	
}
+(TCSGStory *)storyFromDictionary:(NSDictionary *)infoDict 
						 withName:(NSString *)name 
		   inManagedObjectContext:(NSManagedObjectContext *)context
{
    // Create new molecule
	TCSGStory *story;/* = [NSEntityDescription insertNewObjectForEntityForName:@"SGStory" 
	 inManagedObjectContext:context];*/
	/*= [NSEntityDescription insertNewObjectForEntityForName:@"SGStory" 
	 inManagedObjectContext:context];*/
	NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
	NSEntityDescription *entity =[NSEntityDescription entityForName:@"TCSGStory"
											 inManagedObjectContext:context];
	[request setEntity:entity];
	
	NSPredicate *predicate =
	[NSPredicate predicateWithFormat:@"HTMLCode == %@", name];
	[request setPredicate:predicate];
	[request setFetchLimit:1];
	NSError *error = nil;
	NSArray *array = [context executeFetchRequest:request error:&error];
	if (array != nil) {
		int count = [array count]; // may be 0 if the object has been deleted
		int i;
		NSLog(@"not nil");
		NSLog(@"count: %i",count);
		if(count == 0)
		{
			NSLog(@"new item,%@",[infoDict objectForKey:TITLEKEY]);
			story = [NSEntityDescription insertNewObjectForEntityForName:@"TCSGStory" 
												  inManagedObjectContext:context];
			story.lastReadChapter = [NSNumber numberWithInt:3];
		}
		else
		{
			story = [array objectAtIndex:0];
			story.lastReadChapter = [NSNumber numberWithInt:2];
		}
		// …
	}
	else
	{
		story = [NSEntityDescription insertNewObjectForEntityForName:@"TCSGStory" 
											  inManagedObjectContext:context];
		story.host		= [infoDict objectForKey:HOSTKEY];
		story.lastReadChapter = [NSNumber numberWithInt:3];
	}
	
	//NSLog(@"properties: %@", [story properties]);
	//story.name		= name;
	story.title		= [infoDict objectForKey:TITLEKEY];
	story.author	= [infoDict objectForKey:AUTHORKEY];
	story.host		= [infoDict objectForKey:HOSTKEY];
	story.idNumber	= [infoDict objectForKey:IDKEY];
	//story.lastReadChapter = 0;
	NSLog(@"in TCSTory");
	NSLog(@"lastdownloaded chapter: %@",[infoDict objectForKey:LASTDOWNLOADEDCHAPTER]);
	[story setValue:[infoDict objectForKey:LASTDOWNLOADEDCHAPTER] forKey:@"lastDownloadedChapter"];
	story.lastDownloadedChapter = [infoDict objectForKey:LASTDOWNLOADEDCHAPTER];
	checkForUpdates = 0;
	//story.lastDownloadedChapter = [[infoDict objectForKey:LASTDOWNLOADEDCHAPTER] intValue];
	//story.checkForUpdates = NO;
	story.HTMLCode	= name;
	//NSLog(@"properties: %@", [story properties]);
	NSArray *keys	= [infoDict allKeys];
	if([keys containsObject:GENREKEY])
	{
		story.genre	= [infoDict objectForKey:GENREKEY];
	}
	if([keys containsObject:SUMMARYKEY])
	{
		story.summary	= [infoDict objectForKey:SUMMARYKEY];
	}
	if([keys containsObject:CODESKEY])
	{
		story.codes	= [infoDict objectForKey:CODESKEY];
	}
	
    return story;
}
+(TCSGStory *) storyForHTMLCode:(NSString *)HTMLCode withContext:context
{
	TCSGStory *story;
	NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
	NSEntityDescription *entity =[NSEntityDescription entityForName:@"TCSGStory"
											 inManagedObjectContext:context];
	[request setEntity:entity];
	
	NSPredicate *predicate =
	[NSPredicate predicateWithFormat:@"HTMLCode == %@", HTMLCode];
	[request setPredicate:predicate];
	[request setFetchLimit:1];
	NSError *error = nil;
	NSArray *array = [context executeFetchRequest:request error:&error];
	if (array != nil) {
		int count = [array count]; // may be 0 if the object has been deleted
		if(count != 0)
		{
			story = [array objectAtIndex:0];
		}
		// …
	}
	return [story autorelease];
}
+(TCSGStory *) setLastReadChapter:(int)chapterNumber forHTMLCode:(NSString *)HTMLCode inManagedObjectContext:(NSManagedObjectContext *)context
{
	TCSGStory *story;
	NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
	NSEntityDescription *entity =[NSEntityDescription entityForName:@"TCSGStory"
											 inManagedObjectContext:context];
	[request setEntity:entity];
	
	NSPredicate *predicate =
	[NSPredicate predicateWithFormat:@"HTMLCode == %@", HTMLCode];
	[request setPredicate:predicate];
	[request setFetchLimit:1];
	NSError *error = nil;
	NSArray *array = [context executeFetchRequest:request error:&error];
	if (array != nil) {
		int count = [array count]; // may be 0 if the object has been deleted
		NSLog(@"count: %i",count);
		if(count != 0)
		{
			NSLog(@"not nil");
			NSLog(@"found");
			story = [array objectAtIndex:0];
			story.lastReadChapter = [NSNumber numberWithInt:chapterNumber];
		}
		// …
	}
	story.lastDownloadedChapter = [NSNumber numberWithInt:chapterNumber];
	
	NSLog(@"lastreadchapter:%i",[story getLastReadChapter]);
	[story didChangeValueForKey:@"lastReadChapter"];
	[story didChangeValueForKey:@"lastDownloadedChapter"];
	[context deleteObject:story];
	[context insertObject:story];
    if (![context save:&error]) {
        [[NSApplication sharedApplication] presentError:error];
    }
	return story;
}
-(void) setLastReadChapters:(int)chapterNumbers
{
	self.lastReadChapter = [NSNumber numberWithInt:chapterNumbers];
}
-(int)getLastReadChapter
{
	return [self.lastReadChapter intValue];
}

@end
